package com.whut.monitor.controller;

import com.whut.monitor.model.ChannelEntity;
import com.whut.monitor.service.IChannelService;
import com.whut.monitor.vo.ChannelVo;
import com.whut.monitor.vo.ComboVo;
import com.whut.monitor.vo.Grid;
import com.whut.monitor.vo.Tree;
import com.whut.monitor.vo.PageFilter;
import com.whut.monitor.vo.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


/**
 * @author Sandeepin
 * 通道管理接口
 */
@Controller
@RequestMapping("/channel")
public class ChannelController {

	@Autowired
	private IChannelService channelService;

	@RequestMapping("/manager")
	public String manager() {
		return "/mintor/sensorConfiguration/channel";
	}

	@RequestMapping("/dataGrid")
	@ResponseBody
	public Grid dataGrid(ChannelVo channelVo, PageFilter pageFilter) {
		Grid grid = new Grid();
		grid.setRows(channelService.dataGrid(channelVo, pageFilter));
		grid.setTotal(channelService.count(channelVo, pageFilter));
		return grid;
	}


	@RequestMapping("/add")
	@ResponseBody
	public ResponseMsg add(ChannelVo channelVo) {
		ResponseMsg j = new ResponseMsg();
		try {
			channelService.add(channelVo);
			j.setSuccess(true);
			j.setMsg("添加成功！");
		} catch (Exception e) {
			j.setMsg(e.getMessage());
			e.printStackTrace();
		}
		return j;
	}



	@RequestMapping("/edit")
	@ResponseBody
	public ResponseMsg edit(ChannelVo channelVo) {
		ResponseMsg j = new ResponseMsg();
		try {
			channelService.edit(channelVo);
			j.setSuccess(true);
			j.setMsg("编辑成功！");
		} catch (Exception e) {
			j.setMsg(e.getMessage());
			e.printStackTrace();
		}
		return j;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public ResponseMsg delete(Integer id) {
		ResponseMsg j = new ResponseMsg();
		try {
			channelService.delete(id);
			j.setMsg("删除成功！");
			j.setSuccess(true);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
			e.printStackTrace();
		}
		return j;
	}

	@RequestMapping("/tree")
	@ResponseBody
	public List<Tree> tree() {
		return channelService.tree();
	}

	@RequestMapping("/getSensorTypeByEquip")
	@ResponseBody
	public List<ComboVo> getSensorTypeByEquip(Integer equipmentId) {
		List<ComboVo> list=new ArrayList<>();
		List<ChannelEntity> entity=channelService.getSensorTypeByEquip(equipmentId);
		for (ChannelEntity channelEntity : entity) {
			ComboVo comboVo=new ComboVo();
			comboVo.setValue(channelEntity.getSensorEntity().getId());
			comboVo.setText(channelEntity.getSensorEntity().getSensorName());
			comboVo.setNum(channelEntity.getSensorEntity().getSensorNum());
			list.add(comboVo);
		}
		return list;
	}

	@RequestMapping("/getChannelByEquip")
	@ResponseBody
	public List<ComboVo> getChannelByEquip(Integer equipmentId,Integer channelType) {
		List<ComboVo> list=new ArrayList<>();
		List<ChannelEntity> entity=channelService.getChannelByEquip(equipmentId,channelType);
		for (ChannelEntity channelEntity : entity) {
			ComboVo comboVo=new ComboVo();
			comboVo.setValue(channelEntity.getId());
			comboVo.setText(channelEntity.getChannelName());
			comboVo.setNum(channelEntity.getChannelNum());
			list.add(comboVo);
		}
		return list;
	}
}

